/*
Copyright (c) 2008 Mark Rickert

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

package com.mileage_tracker.util
{
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.net.registerClassAlias;
    import flash.utils.Dictionary;
    import flash.utils.Endian;
    import flash.utils.IDataInput;
    import flash.utils.IDataOutput;
    import flash.utils.IExternalizable;
    
    public class WindowPrefs
    {
        
        private static const PREFS_FILENAME:String        = 'prefs.defaults';
        
        private static var _prefValues:Dictionary;
        
        /**
         * Creates out static instance
         **/
        static public function init():void
        {
            if ( null == _prefValues ) {
                _prefValues = new Dictionary();
                loadPreferences();
            }
            
        }
        
        /**
         * Load the preferences file
         */
        static private function loadPreferences():void {
    
            init();
            
            var fp: File = File.applicationStorageDirectory;
            
            fp = fp.resolvePath( PREFS_FILENAME );
            
            if ( fp.exists ) {
                
                var    _prefsStream:FileStream;
                _prefsStream = new FileStream();
                _prefsStream.open( fp, FileMode.READ);
                _prefsStream.endian = Endian.BIG_ENDIAN;
                
                readExternal( _prefsStream );
                _prefsStream.close();
                
            } else {
                savePreferences();
            }

        }
        
        /**
         * Saves the preferences file
         **/
        static private function savePreferences():void {

            init();

            var fp: File = File.applicationStorageDirectory;
            
            fp = fp.resolvePath( PREFS_FILENAME );
            
            var _prefsStream:FileStream;
            _prefsStream = new FileStream();
            _prefsStream.open( fp, FileMode.WRITE );
            _prefsStream.endian = Endian.BIG_ENDIAN;
            
            writeExternal( _prefsStream );
            _prefsStream.close();
            
        }
        
        /**
         * Sets a preferences value
         **/
        static public function setPreference( name:String, value:Object ):void {
            
            init();

            _prefValues[ name ] = value;
            savePreferences();
            
        }
        
        /**
         * Sets an <int> preferences value
         **/
        static public function setInt( name:String, value:int ):void {
            setPreference( name, value );
        }

        /**
         * Get a preferences values
         **/
        static public function getPreference( name:String ):* {
            
            init();

            var value:Object;
    
            try {
                
                value = _prefValues[ name ];
                
            } catch( e:Error ) {
                
                value = null;
                
            }
            
            return value;
            
        } // getPreference
        
        /**
         * Get an <b>int</b> preferences values
         **/
        static public function getInt( name:String ):int {
            
            var value:* = getPreference( name );
            
            if ( null == value ) {
                return 0;
            } else {
                return value as int;
            }
        }
        
        /**
         * Loads serializes preferences data structure from <b>input:IDataInput</b>.
         **/
        static private function readExternal(input:IDataInput):void
        {
        
            registerClassAlias( "flash.utils.Dictionary", Dictionary );
            _prefValues = input.readObject() as Dictionary;
            
        } // readExternal
    
        /**
         * Writes preferences data as a serialized structure to <b>output:IDataOutput</b>.
         **/
        static private function writeExternal(output:IDataOutput):void
        {

            registerClassAlias( "flash.utils.Dictionary", Dictionary );
            output.writeObject( _prefValues );
            
        } // writeExternal

    } // class


} // package